//
//  Macro_Capabilities.swift
//  Voice
//
//  Created by 卢逢卫 on 2024/4/3.
//

import AVFoundation
import Photos
import UIKit
import CoreLocation

public enum Service_Type {
    case camera
    case photo
    case location
}

public struct Macro_Capabilities {
    public static func allow_Service(type: Service_Type) -> Bool {
        // 实现权限开启判断逻辑
        switch type {
        case .camera:
            // 判断相机权限是否允许
            // 返回相机权限是否允许的结果
            return checkCameraPermission()
            
        case .photo:
            // 判断相册权限是否允许
            // 返回相册权限是否允许的结果
           return checkPhotoLibraryPermission()
        case .location:
            // 判断定位权限是否允许
            // 返回定位权限是否允许的结果
            return checkLocationPermission()
        }
      
    }

    public static func settings_Service(controller: UIViewController, type: Service_Type, msg: String) {
        // 打开权限弹框，提示用户是否开启权限
        switch type {
        case .camera:
            // 显示相机权限弹框
            // 弹框中显示msg作为提示信息
            let alertController = UIAlertController(title: "相机", message: msg, preferredStyle: .alert)
                    
            let settingsAction = UIAlertAction(title: "现在去开启", style: .default) { _ in
                        openSettings()
            }
            alertController.addAction(settingsAction)
                    
            controller.present(alertController, animated: true, completion: nil)
            break
        case .photo:
            // 显示相册权限弹框
            // 弹框中显示msg作为提示信息
            let alertController = UIAlertController(title: "照片", message: msg, preferredStyle: .alert)
                    
            let settingsAction = UIAlertAction(title: "现在去开启", style: .default) { _ in
                        openSettings()
            }
            alertController.addAction(settingsAction)
                    
            controller.present(alertController, animated: true, completion: nil)
            break
        case .location:
            // 显示定位权限弹框
            // 弹框中显示msg作为提示信息
            let alertController = UIAlertController(title: "位置", message: msg, preferredStyle: .alert)
                    
            let settingsAction = UIAlertAction(title: "现在去开启", style: .default) { _ in
                        openSettings()
            }
            alertController.addAction(settingsAction)
                    
            controller.present(alertController, animated: true, completion: nil)
            break
        }
    }
    
    private static func openSettings() {
            guard let settingsURL = URL(string: UIApplication.openSettingsURLString) else {
                return
            }
            
            if UIApplication.shared.canOpenURL(settingsURL) {
                UIApplication.shared.open(settingsURL, options: [:], completionHandler: nil)
            }
        }
    
    
        private static func checkCameraPermission() -> Bool {
            let authorizationStatus = AVCaptureDevice.authorizationStatus(for: .video)
            return authorizationStatus == .authorized
        }
        
        private static func checkPhotoLibraryPermission() -> Bool {
            let authorizationStatus = PHPhotoLibrary.authorizationStatus()
            return authorizationStatus == .authorized
        }
    
        private static func checkLocationPermission() -> Bool {
            let locationManager = CLLocationManager()
            let authorizationStatus = CLLocationManager.authorizationStatus()
            return authorizationStatus == .authorizedWhenInUse || authorizationStatus == .authorizedAlways
        }
}
